package com.itsu.controller;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageInfo;
import com.itsu.domain.ModuleTree;
import com.itsu.domain.Role;
import com.itsu.service.Impl.RoleServiceImpl;
import com.itsu.service.RoleService;
import com.itsu.utils.BeanUtil;
import com.mysql.cj.util.StringUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@WebServlet("/system/role")
public class RoleController extends HttpServlet {

    private RoleService roleService = new RoleServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String operation = req.getParameter("operation");
        if ("list".equals(operation)){
            showList(req, resp);
        } else if ("toAdd".equals(operation)) {
            toAdd(req, resp);
        } else if ("save".equals(operation)) {
            save(req, resp);
        } else if ("toEdit".equals(operation)) {
            toEdit(req, resp);
        } else if ("edit".equals(operation)) {
            edit(req, resp);
        } else if ("delete".equals(operation)) {
            delete(req, resp);
        } else if ("toAuthor".equals(operation)) {
            toAuthor(req, resp);
        } else if ("updateRoleModule".equals(operation)) {
            updateRoleModule(req, resp);
        }
    }

    private void showList(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        int pageNum = 1;
        int pageSize = 10;
        if (!StringUtils.isNullOrEmpty(req.getParameter("page"))){
            pageNum = Integer.parseInt(req.getParameter("page"));
        }
        PageInfo<Role> pageInfo = roleService.findAll(pageNum, pageSize);

        req.setAttribute("page", pageInfo);

        req.getRequestDispatcher("/WEB-INF/pages/system/role/list.jsp").forward(req, resp);
    }

    private void toAdd(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getRequestDispatcher("/WEB-INF/pages/system/role/add.jsp").forward(req, resp);
    }

    private void save(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Role role = BeanUtil.fillBean(req, Role.class);
        role.setId(UUID.randomUUID().toString());
        role.setCreateTime(new Date());
        roleService.save(role);
        resp.sendRedirect(req.getContextPath()+"/system/role?operation=list");
    }


    private void toEdit(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        Role role = roleService.findById(id);
        req.setAttribute("role", role);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/update.jsp").forward(req, resp);
    }

    private void edit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Role role = BeanUtil.fillBean(req, Role.class);
        roleService.update(role);

        resp.sendRedirect(req.getContextPath()+"/system/role?operation=list");
    }

    private void delete(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Role role = BeanUtil.fillBean(req, Role.class);
        roleService.delete(role);

        resp.sendRedirect(req.getContextPath()+"/system/role?operation=list");
    }

    private void toAuthor(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String roleId = req.getParameter("id");
        List<ModuleTree> treeList = roleService.getModuleByRoleId(roleId);
        ObjectMapper objectMapper = new ObjectMapper();
        String treeJson = objectMapper.writeValueAsString(treeList);

        Role role = roleService.findById(roleId);
        req.setAttribute("json", treeJson);
        req.setAttribute("role", role);
        req.getRequestDispatcher("/WEB-INF/pages/system/role/author.jsp").forward(req, resp);
    }

    private void updateRoleModule(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String roleId = req.getParameter("roleId");
        String moduleIds = req.getParameter("moduleIds");
        String[] split = moduleIds.split(",");
        //删除模块和角色的关联关系
        roleService.deleteModuleByRoleId(roleId);


        for (String moduleId : split) {
            //添加模块和角色的关联关系
            roleService.addModuleRole(roleId, moduleId);
        }

        resp.sendRedirect(req.getContextPath()+"/system/role?operation=list");
    }
}
